﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace FrameworkXNA
{
    static class Controller
    {
        static KeyboardState previousKeyboardState, currentKeyboardState;
        static MouseState previousMouseState, currentMouseState;

        //X-Coordinate of the mouse cursor
        public static int MouseX
        {
            get { return currentMouseState.X; }
            set { Mouse.SetPosition(value, currentMouseState.X); }
        }

        //Y-Coordinate of the mouse cursor
        public static int MouseY
        {
            get { return currentMouseState.Y; }
            set { Mouse.SetPosition(currentMouseState.Y, value); }
        }

        public static Point mousePoint()
        {
            return new Point(MouseX, MouseY);
        }

        public static bool MouseLeftDown()
        {
            return (currentMouseState.LeftButton == ButtonState.Pressed);
        }

        public static bool MouseRightDown()
        {
            return (currentMouseState.RightButton == ButtonState.Pressed);
        }

        public static bool MouseMiddleDown()
        {
            return (currentMouseState.MiddleButton == ButtonState.Pressed);
        }
        
        public static bool MouseLeftPressed()
        {
            return ((currentMouseState.LeftButton == ButtonState.Pressed) && (previousMouseState.LeftButton == ButtonState.Released));
        }

        public static bool MouseRightPressed()
        {
            return ((currentMouseState.RightButton == ButtonState.Pressed) && (previousMouseState.RightButton == ButtonState.Released));
        }

        public static bool MouseMiddlePressed()
        {
            return ((currentMouseState.MiddleButton == ButtonState.Pressed) && (previousMouseState.MiddleButton == ButtonState.Released));
        }

        public static bool MouseLeftReleased()
        {
            return ((currentMouseState.LeftButton == ButtonState.Released) && (previousMouseState.LeftButton == ButtonState.Pressed));
        }

        public static bool MouseRightReleased()
        {
            return ((currentMouseState.RightButton == ButtonState.Released) && (previousMouseState.RightButton == ButtonState.Pressed));
        }

        public static bool MouseMiddleReleased()
        {
            return ((currentMouseState.MiddleButton == ButtonState.Released) && (previousMouseState.MiddleButton == ButtonState.Pressed));
        }

        public static bool keyDown(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key);
        }

        public static bool keyPressed(Keys key)
        {
            return (currentKeyboardState.IsKeyDown(key) && previousKeyboardState.IsKeyUp(key));
        }

        public static bool keyReleased(Keys key)
        {
            return (currentKeyboardState.IsKeyUp(key) && previousKeyboardState.IsKeyDown(key));
        }

        public static void Update(GameTime gameTime)
        {
            previousKeyboardState = currentKeyboardState;
            previousMouseState = currentMouseState;
            currentKeyboardState = Keyboard.GetState();
            currentMouseState = Mouse.GetState();
        }
    }
}
